i = 10
j = 0
# ZeroDivisionError 除零异常
# print(i / j)

print(i)
print(j)
# NameError 名称异常
# print(k)

# TypeError 类型异常
# print(1 + "2")

# ValueError 值异常
# int('abc')

list1 = [1, 2, 3, 4]
print(list1[0])
print(list1[1])
print(list1[3])


# IndexError 索引异常
# print(list1[4])

# AttributeError 属性异常
# list1.split()


# 自定义异常
class AgeError(Exception):
    pass


# 处理异常
try:
    # 当年龄<0或者>200时 抛出一个AgeError
    age = 20
    if age < 0 or age > 200:
        # 手动抛出异常
        raise AgeError("年龄错误")
    print(10 / 0)
# 注意异常捕获的先后顺序
except Exception as e:
    print(e)
    print("Exception是所有异常类的父类，所有后面捕获异常的代码都会被忽略")
except AgeError as e:
    print(e)
except ZeroDivisionError as e:
    print(e)
    print("请检查除数！")
else:
    print("当没有异常发生时则会执行")
finally:
    print("不管有没有发生异常都会执行")
